package com.qc31.monitorlib.ui.stopcar

import autodispose2.autoDispose
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.entity.StopStats
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.base.BaseReportViewModel
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/** @Author Jasper @Time 2020/12/11 @Desc 停车统计 */
class StopCarViewModel : BaseReportViewModel(isThirty = true, isSimpleThirty = true) {
    //停车时长
    var duration = "0"

    //ACC开关
    var switchAcc = false

    private val dataSub by lazy { PublishSubject.create<StopStats>() }
    fun dataObserver(): Observable<StopStats> = dataSub.hide()

    override fun isParamsOk(): Boolean {
        if (duration.isEmpty()) {
            duration = "0"
//            toast(R.string.toast_hint_stop_long)
//            return true
        }
        return super.isParamsOk()
    }

    override fun getData(carId: String, startTime: String, endTime: String) {
        toastLoading(true)
        ServiceHelper.getRepository().getStopData(
            carId,
            TimeUtil.instance.formatHms(startTime),
            TimeUtil.instance.formatEndHms(endTime),
            TimeUtil.instance.formatTime(duration.toInt() * 60),
            switchAcc
        ).autoDispose(this).subscribe({
            toastLoading(false)
            if (isHasListData(it.list, R.string.toast_no_stop_data)) return@subscribe
            dataSub.onNext(it)
        }, error)
    }

}
